#!/bin/bash
#
# USAGE:
#       p singleMasterStack_Parallel.adr
#
# DESCRIPTION:
#  singleMasterStack_Parallel.adr is an ADORE script.  
#  It generates a single master stack interferograms using the ADORE settings. 
#  It is very similar to the singleMasterStack.adr script. However, this script 
#  uses the Torque-PBS system to distribute jobs to other computers.
#
# INPUT:
#  There are no input parameters. 
#  
# OUTPUT:
#  Creates ${projectFolder}/process/${runName}/ and processes crops & interferograms
#  It runs the following DORIS steps:
#    For crops: s_readfiles, s_porbits, s_crop, s_ovs 
#    For i12s: coarseorb, coarsecorr, fine, coregpm, coregpm, resample, interfero, comprefpha, subtrrefpha
#  It also creates an amplitude raster for resample and a phase raster for subtrrefpha.
#

function parallelProcess(){
# $1:outputFolder
# $2:adore command
# expects settings to be at outputFolder/settings.set
cat >${1}/batch.sh<<_EOFFILE
#! /bin/csh
 #PBS -N parallelJob
 #PBS -l nodes=1
 #PBS -q batch
 #PBS -V
 #PBS -l walltime=5:00:00
 cd ${1}
 ${ADORESCR}/adore -u "${1}/settings.set" "${2}"
_EOFFILE

#submit the job to the que
qsub ${1}/batch.sh
}

# we will be changing directories
# save the current dir.
CALLDIR=$PWD;
# DORIS does not allow master and slave to be the same. 
# We assign master as dummy ans slave as the current scene.      
stackMaster=${master};
settings apply -r master="dummy"

for scene in ${scenes_include[*]}
do
     bold "Processing ${scene}"
     settings apply -r slave=${scene} outputFolder="${cropsFolder}/${scene}"
     mkdir -p "${outputFolder}"
     cd "${outputFolder}"    
     settings save

     parallelProcess "${outputFolder}" "s_readfiles;s_porbits;s_crop;s_ovs"
     
done

while [ 1 ];
do
  #wait until last submitted job creates the ovs file. 
  if [ -e "${outputFolder}/${slave}.ovs" ]; then
    break;
  else
    sleep 10
  fi
done

####Now the i12s
#Set the stack master.
settings apply -r master=${stackMaster}

for scene in ${scenes_include[*]}
do
   if [ "${master}" == "${scene}" ]; then
     continue
   fi
     bold "Processing ${master}_${scene}"
     settings apply -r slave=${scene} outputFolder="${i12sFolder}/${master}_${scene}"
     mkdir -p "${outputFolder}"
     cd "${outputFolder}"
     settings save
     cp ${cropsFolder}/${master}/${master}.res ${outputFolder}/${master}.res
     cp ${cropsFolder}/${slave}/${slave}.res ${outputFolder}/${slave}.res

     parallelProcess "${outputFolder}" "coarseorb;coarsecorr;fine;coregpm;resample;interfero;comprefpha;subtrrefpha;raster a resample -M1/5;raster p subtrrefpha -M1/5"
done


#go back to where we started
cd ${CALLDIR}

